Test the if-else execution efficiency.
Test environment: Mac pro i7 2.3 Ghz... Gcc 4.9 compiler, code not optimized-O0;
Test code: c code 1:
int main(){int n=100000000;int b=-1;int a=0;while(n-->0){if(b
Assembly Code 1:
_main:LFB0:pushl%ebpLCFI0:movl%esp, %ebpLCFI1:subl$16, %espmovl$100000000, -4(%ebp)movl$-1, -8(%ebp)movl$0, -12(%ebp)jmpL2L22:cmpl$0, -8(%ebp)jnsL3subl$1, -8(%ebp)jmpL4L3:subl$1, -8(%ebp)L4:cmpl
After reviewing the compilation principles, yundun issued a question like this:SourceProgram:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 Void F ( Int A, Int B, Int C, Int D, Int X, Int Y, Int Z) 2 { 3 While ( B) 4 { 5 If (C D) 6 X = X + Z; 7 Else 8 X = Y - Z; 9 } 10 }
The maximum optimization compiled with GCC 3.3.5 is:
Code highlighting produced by Actipro CodeHighl
This article explains the use of assembly language to find the maximum value of a set of numbers, mainly related to the knowledge points have data segments, loops and so on.# Purpose: Find the largest number in the program # # Variable: The purpose of the Register # %edi-Save the data item index being checked # %EBX-the largest item currently found # %EAX-Current Data # # Use the following memory location: # Data_ Items-Contains data item # 0 indicates
. Now, let's go to the center. Please enter schedule ().
4. Schedule (). (Document 3: sched. C: Schedule ())
Void schedule (void){Int next;...Switch_to (next );}
Oh, here I have omitted a few more codes. It executes the scheduling algorithm, that is, to find the next process to be executed from all the processes in the 'run' state, then, assign the number to next. Switch!
Switch_to () is a macro defined in (document 3: sched. h:
# Define switch_to (n ){/Struct (long a, B;} _ TMP ;/_ ASM _ ("
/*************************************** * *****/Text: machine-level representation of C program Author: Arden Chao Date: 2006-10-17 Email: arden1019@gmail.com version: 1.0.0 /************************************** * ***** // 2006-10-25 falls into the addressing mode ///////// //////// the addressing mode of the computer is very complicated (I think ). I am also in this situation. I will not write it today.
//// //-Process Control //////////////////
North cannot be found. I don't know how to pro
optimization ah. [email protected] test]$ gcc-s-o test.c
Add_counter:
PUSHL%EBP
MOVL%esp,%EBP
MOVL counter,%eax
Cmpl $16,%eax
JE. L4
. L5:
Addl,%eax
Cmpl $16,%eax
Jne. L5
MOVL%eax, counter
. L4:
POPL%EBP
Ret
from the assembly code above, it is clear that the value of the counter is stored in eax, then 2 is added to eax each time, and then us
-point number, we call the Emitx87branch function on line 8th to process. When the operand is an integer, we will make a further judgment in line 11th. Since constants are present in the code area in the form of "immediate number", when the program is run, the CPU reads the machine instruction from the code area and loads the immediate number into the CPU, so when the operand SRC2 is a constant, we do not have to load the SRC1 value into the register, which does not violate the " The two operand
Original posts:
MOVL $girl _friend, $bed
jum bed
bed: Call
take_off_clothes
xorl%ecx,%ecx love
:
cmpl $1024,% Ecx JE over push pop push pop
push pops
incl%ecx
goto
Love over: Call
sleep_for_30seconds
jmp Bed
Architects:
MOVL $girl _friend, $bed
jum bed
bed: Call
take_off_clothes
xorl%ecx,%ecx love
:
Cmpl $ 3072,%ecx
JE over
push
pops
incl%ecx
goto love
over:call sleep_for_30seconds
JMP Bed
C
condition satisfies for example the JE Condition code is the ZF this bit is 1 means that the comparison is a zero is called result 0 or The result is equal, so in this case, jump on it, or we'll go in sequence.
What about the jne just like that? and the JS Jns is similar, of course. JA JB JG JL we talked about it. There is a set that is used for this signed integer comparison. The two sets that we want to differentiate between the two sets we have to distinguish from each other. Of course, it's
of the EDI element in the array should be data_items + EDI * 4.
Start_loop indicates the start loop, and loop_exit indicates the end loop.
CMPL $0, % eaxThe CMPL command subtract two operands, but the calculation result is not saved. It only changes the flag bit in the eflags register based on the calculation result. If the two operands are equal, the calculation result is 0, and the ZF position in eflags
1. Top priority-algorithm optimization:The most significant Optimization Method for program performance optimization is algorithm optimization. The performance improvement of algorithm optimization is usually an order of magnitude, such as sorting, the time complexity of bubble is O (n ^ 2), and the time complexity of fast sorting is O (nlog (N). This performance is very obvious.2. eliminate redundant cycles:Let's take a look at the assembly code for (INT n = 4, I = 0; I 15 movl $4,-4 (% EBP) //
by N and A, and the value returned by the called function is the required value. This function has no effect, therefore, this function is no longer saved and is directly called recursively on the function stack,In special cases, you can do this directly in registers without using the memory space.
How does the compiler determine whether tail recursion works?The returned value is the function, and there is no other choice.
Let's take a look at the compilation results of the above-mentioned tail
pointed to by faulty_instruction_address. It usually returns an error code to the user program.
For example, in the kernel code that accesses the process address space:
__get_user_1: GET_THREAD_INFO(%edx) cmpl TI_addr_limit(%edx),%eax jae bad_get_user1: movzbl (%eax),%edx xorl %eax,%eax ret__get_user_2: addl $1,%eax jc bad_get_user GET_THREAD_INFO(%edx) cmpl TI_addr_limit(%edx
In CL we can do this:
$ SBCL
* (+ 1 2 3)
6
* (
From a simple perspective, CL's + and
So when the c++11 begins to have the variable parameter template, you can play multiple parameter addition and multiple parameter comparisons.
#include
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/
Some people may say, this is not a pit dad, regardless of the wording than CL ugly, you also recursive call, simply weak burst ...
Let's take a look at the Truth (c
In CL, we can do this:
1 $ sbcl2 * (+ 1 2 3)3 64 * (
In simple terms, the + and Therefore, after the variable parameter template is available in C ++ 11, you can add multiple parameters and compare multiple parameters.
1 #include
Some people may say that this is not a pitfall. no matter whether the writing method is ugly than CL, you still call it recursively. It's so weak .....
Let's look at the truth (clang ):
1 foo(int, int, int, int): # @foo(int, int, int, int)
bytes. The address of the EDI element in the array should be data_items + EDI * 4.
Start_loop indicates the start loop, and loop_exit indicates the end loop.
CMPL $0, % eaxThe CMPL command subtract two operands, but the calculation result is not saved. It only changes the flag bit in the eflags register based on the calculation result. If the two operands are equal, the calculation result is 0, and the
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.